package by.dkusch.aircompany.services.csv.load;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.BindyType;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import by.dkusch.aircompany.services.csv.airportcsv.AirportCSV;
import by.dkusch.aircompany.services.csv.countrycsv.CountryCSV;
import by.dkusch.aircompany.services.csv.regioncsv.RegionCSV;

@Component
public class CsvRoute extends RouteBuilder {
	private static final long BATCH_TIME_OUT = 60000L;
	@Autowired
	private CsvToModelMapper csvToModelConverter;
	@Autowired
	private CsvRouteStarter csvRouteStarter;
	private boolean auto = false;

	@Override
	public void configure() throws Exception {
		from("file://src/data?noop=true&fileName=deleteall.csv&delete=true").autoStartup(!auto)
				.id("deleteRoute").startupOrder(1).log("Start cleaning ....")
				.bean(csvToModelConverter.getAirportService(), "deleteAll")
				.bean(csvToModelConverter.getRegionService(), "deleteAll")
				.bean(csvToModelConverter.getCountryService(), "deleteAll")
				.log("End cleaning ....").bean(csvRouteStarter, "startCountry");

		/* Country */
		from("file://src/data?noop=true&fileName=country.csv").autoStartup(auto).id("countryRoute")
				.log("Start processing ....").to("direct:database-save-country").end();

		from("direct:database-save-country").log("Start saving countries to database ....").split()
				.tokenize("\n").streaming().unmarshal().bindy(BindyType.Csv, CountryCSV.class)
				.bean(csvToModelConverter, "convertToDBCountry")
				.aggregate(constant(true), batchAggregationStrategy())
				.completionTimeout(BATCH_TIME_OUT)
				.bean(csvToModelConverter.getCountryService(), "saveList")
				.log("End saving to database ....").bean(csvRouteStarter, "startRegion").end();

		/* Region */
		from("file://src/data?noop=true&fileName=regions.csv").autoStartup(auto).id("regionRoute")
				.log("Start processing ....").to("direct:database-save-region").end();

		from("direct:database-save-region").log("Start saving regions to database ....").split()
				.tokenize("\n").streaming().unmarshal().bindy(BindyType.Csv, RegionCSV.class)
				.bean(csvToModelConverter, "convertToDBRegion")
				.aggregate(constant(true), batchAggregationStrategy())
				.completionTimeout(BATCH_TIME_OUT)
				.bean(csvToModelConverter.getRegionService(), "saveList")
				.log("End saving to database ....").bean(csvRouteStarter, "startAirport").end();

		/* Airport */
		from("file://src/data?noop=true&fileName=airport.csv").autoStartup(auto).id("airportRoute")
				.log("Start processing 40....").to("direct:database-save-airport").end();

		from("direct:database-save-airport").log("Start saving airports to database ....").split()
				.tokenize("\n").streaming().unmarshal().bindy(BindyType.Csv, AirportCSV.class)
				.bean(csvToModelConverter, "convertToDBAirport")
				.aggregate(constant(true), batchAggregationStrategy())
				.completionTimeout(BATCH_TIME_OUT)
				.bean(csvToModelConverter.getAirportService(), "saveList")
				.log("End saving to database ....").end();

	}

	@Bean
	private AggregationStrategy batchAggregationStrategy() {
		return new ArrayListAggregationStrategy();
	}

}
